package com.qst.yunpan.interceptor;

import com.qst.yunpan.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置拦截器，拦截未登录用户
 * @author zhaohualuo
 * @version 2019年5月8日
 * @since 1.0
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        //执行完毕，返回前拦截
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        //处理过程中执行拦截
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        //在拦截点执行前拦截，如果返回true则不执行拦截点后的操作（拦截成功）
        //返回false则不执行拦截
        String url=request.getRequestURI();
        if (url.indexOf("customer/user/todologin") >= 0 || url.indexOf("customer/share.action")>=0||
                url.indexOf("customer/user/login.action")>=0||url.indexOf("customer/user/toregister")>=0 ||
                url.indexOf("customer/user/register.action")>=0 ||url.indexOf("customer/file/download.action")>=0){
            return true;
        }else{
            User user = (User) request.getSession().getAttribute("user");
            if (user!=null){
                return true;
            }
        }


        response.sendRedirect("/customer/user/todologin");
        return false;
    }

}

